<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属企业" prop="ownedCompany">
        <el-input
          v-model="queryParams.ownedCompany"
          placeholder="请输入所属企业"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作状态" prop="status">
          <el-select clearable v-model="queryParams.status"  placeholder="请选择状态">
            <el-option v-for="item in statusList" :key="item.val" :label="item.label" :value="item.val">		</el-option>
          </el-select>
      </el-form-item>
      <!--<el-form-item label="安装日期" prop="installTime">-->
        <!--<el-date-picker clearable size="small"-->
          <!--v-model="queryParams.installTime"-->
          <!--type="date"-->
          <!--value-format="yyyy-MM-dd"-->
          <!--placeholder="选择安装日期">-->
        <!--</el-date-picker>-->
      <!--</el-form-item>-->
      <el-form-item label="责任人" prop="responsible">
        <el-input
          v-model="queryParams.responsible"
          placeholder="请输入责任人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['environment:opmEnvEnterpriseEquipment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['environment:opmEnvEnterpriseEquipment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['environment:opmEnvEnterpriseEquipment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['environment:opmEnvEnterpriseEquipment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="opmEnvEnterpriseEquipmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" align="center" prop="id" /> -->
      <el-table-column label="设备名称" align="center" prop="name" />
      <!--<el-table-column label="设备编号" align="center" prop="facilityNum" />-->
      <el-table-column label="所属企业" align="center" prop="ownedCompany" />
      <!--<el-table-column label="设备分类"  align="center" prop="deviceType" />-->
      <el-table-column label="工作状态" align="center" prop="status" :formatter="typeFormatter" />
      <el-table-column label="安装位置" align="center" prop="location" />
      <el-table-column label="安装日期" align="center" prop="installTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.installTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="responsible" />
      <el-table-column label="联系电话" align="center" prop="respPhone" />
      <el-table-column label="设备说明" align="center" prop="desc1" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['environment:opmEnvEnterpriseEquipment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['environment:opmEnvEnterpriseEquipment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="设备编号" prop="facilityNum">-->
          <!--<el-input v-model="form.facilityNum" placeholder="请输入设备编号" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="设备分类" v-if="!form.id">-->
          <!--<treeselect v-model="form.sysBmId" :options="sysBmTypeOptions" :normalizer="bmNormalizer"-->
                      <!--@select="genCode" placeholder="设备分类"/>-->
        <!--</el-form-item>-->
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="所属企业" prop="ownedCompany">
          <el-select clearable v-model="form.ownedCompany" filterable placeholder="请选择所属企业">
            <el-option v-for="item in qyEnterpriseList" :key="item.id" :label="item.enterpriseName"
              :value="item.enterpriseName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作状态">
          <el-select clearable v-model="queryParams.status"  placeholder="请选择状态">
            <el-option v-for="item in statusList" :key="item.val" :label="item.label" :value="item.val"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="安装位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入安装位置" />
        </el-form-item>
        <el-form-item label="安装日期" prop="installTime">
          <el-date-picker clearable size="small"
            v-model="form.installTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择安装日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="责任人" prop="responsible">
          <el-input v-model="form.responsible" placeholder="请输入责任人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="respPhone">
          <el-input v-model="form.respPhone" placeholder="请输入责任人电话" />
        </el-form-item>
        <el-form-item label="设备说明" prop="desc1">
          <el-input v-model="form.desc1" placeholder="请输入设备说明" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOpmEnvEnterpriseEquipment, getOpmEnvEnterpriseEquipment, delOpmEnvEnterpriseEquipment, addOpmEnvEnterpriseEquipment, updateOpmEnvEnterpriseEquipment } from "@/api/smartParks/environment/opmEnvEnterpriseEquipment";
import {listQyEnterprise,} from "@/api/smartParks/merchants/merchantsCompanySettlein";
export default {
  name: "OpmEnvEnterpriseEquipment",
  components: {
  },
  data() {
    return {
      statusList:[{label:'正常',val:'0'},{label:'停用',val:'1'}],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业设备表格数据
      opmEnvEnterpriseEquipmentList: [],
      qyEnterpriseList:[],
      sysBmTypeOptions:this.$store.state.permission.sysBmTree,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ownedCompany: null,
        name: null,
        status: null,
        installTime: null,
        responsible: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ownedCompany: [
          { required: true, message: "所属企业不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "工作状态不能为空", trigger: "blur" }
        ],
        location: [
          { required: true, message: "安装位置不能为空", trigger: "blur" }
        ],
        responsible: [
          { required: true, message: "责任人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    listQyEnterprise().then((response) => {
      this.qyEnterpriseList = response.rows;
    });
  },
  methods: {
    /** 查询企业设备列表 */
    getList() {
      this.loading = true;
      listOpmEnvEnterpriseEquipment(this.queryParams).then(response => {
        this.opmEnvEnterpriseEquipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    typeFormatter: function(row) {
      let status = row.status
      for (var i = 0; i < this.statusList.length; i++) {
        if (this.statusList[i].val == status) {
          return this.statusList[i].label;
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ownedCompany: null,
        name: null,
        facilityNum: null,
        status: "0",
        location: null,
        installTime: null,
        responsible: null,
        respPhone: null,
        desc1: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOpmEnvEnterpriseEquipment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOpmEnvEnterpriseEquipment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOpmEnvEnterpriseEquipment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除企业设备编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOpmEnvEnterpriseEquipment(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('environment/opmEnvEnterpriseEquipment/export', {
        ...this.queryParams
      }, `environment_opmEnvEnterpriseEquipment.xlsx`)
    }
  }
};
</script>
